package org.qiyu.live.api.service.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.qiyu.live.api.service.ILivingRoomService;
import org.qiyu.live.api.vo.req.LivingRoomReqVO;
import org.qiyu.live.api.vo.req.OnlinePkReqVO;
import org.qiyu.live.api.vo.resp.LivingRoomPageResp;
import org.qiyu.live.api.vo.resp.LivingRoomRespVo;
import org.qiyu.live.common.interfaces.utils.ConvertBeanUtils;
import org.qiyu.live.common.interfaces.vo.PageWrapper;
import org.qiyu.live.user.dto.UserDTO;
import org.qiyu.live.user.interfaces.IUserRpc;
import org.qiyu.live.web.starter.context.QiyuRequestContext;
import org.qiyu.live.web.starter.error.ErrorAssert;
import org.qiyu.live.web.starter.error.QiYuBaseErrorEnum;
import org.springframework.stereotype.Service;
import qiyu.live.living.dto.req.LivingRoomReqDTO;
import qiyu.live.living.dto.req.OnlinePkReqDTO;
import qiyu.live.living.dto.resp.LivingRoomRespDTO;
import qiyu.live.living.interfaces.ILivingRoomRpc;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-10-24
 * Time: 19:46
 */
@Service
public final class LivingRoomServiceImpl implements ILivingRoomService {

    @DubboReference
    private ILivingRoomRpc livingRoomRpc;

    @DubboReference
    private IUserRpc userRpc;

    /**
     * 这里需要远程调用living服务插入
     *
     * @param type 直播间类型
     * @return
     */
    @Override
    public boolean startLivingRoom(Integer type) {
        LivingRoomReqDTO livingRoomReqDTO = new LivingRoomReqDTO();
        Long userId = QiyuRequestContext.getUserId();
        UserDTO userInfo = userRpc.getUserInfo(userId);
        livingRoomReqDTO.setType(type);
        livingRoomReqDTO.setRoomName("主播-" + userId + "的直播间");
        livingRoomReqDTO.setAnchorId(userId);
        livingRoomReqDTO.setCovertImg(userInfo.getAvatar());
        return livingRoomRpc.startLivingRoom(livingRoomReqDTO) > 0;
    }

    /**
     * 这里需要远程调用living服务插入
     *
     * @param roomId 直播间类型
     * @return
     */
    @Override
    public boolean closeLivingRoom(Integer roomId) {
        LivingRoomReqDTO livingRoomReqDTO = new LivingRoomReqDTO();
        livingRoomReqDTO.setRoomId(roomId);
        return livingRoomRpc.closeLivingRoom(livingRoomReqDTO);
    }

    /**
     * @param livingRoomReqVo vo
     * @return 返回直播间列表
     */
    @Override
    public LivingRoomPageResp list(LivingRoomReqVO livingRoomReqVo) {
        PageWrapper<LivingRoomRespDTO> pageWrapper = livingRoomRpc.list(ConvertBeanUtils.convert(livingRoomReqVo, LivingRoomReqDTO.class));
        LivingRoomPageResp livingRoomPageResp = new LivingRoomPageResp();
        livingRoomPageResp.setList(ConvertBeanUtils.convertList(pageWrapper.getList(), LivingRoomRespVo.class));
        livingRoomPageResp.setHasNext(pageWrapper.isHasNext());
        return livingRoomPageResp;
    }

    /**
     * 记录pk的一方
     *
     * @param onlinePkReqVO
     * @return
     */
    @Override
    public boolean onlinePk(OnlinePkReqVO onlinePkReqVO) {
        boolean isSuccess = livingRoomRpc.onlinePk(ConvertBeanUtils.convert(onlinePkReqVO, OnlinePkReqDTO.class));
        ErrorAssert.isNotBlank(isSuccess, QiYuBaseErrorEnum.LIVING_PK_BUSY);
        return true;
    }
}
